import { Component, ElementRef, Renderer2, ViewChild } from '@angular/core';
import { NavController, NavParams, ToastController, AlertController, App, IonicPage } from 'ionic-angular';
import { Api } from "../../providers/api/api";
import { QueOutput, QueInfo, RowInfo, ColumnInfo, FType, DropdownInfo, TopicResultInfo, ListChildInfo, AnswerLog } from '../../models/quesinfo';
import { localbase } from '../../models/localbase';
import { CityDataProvider } from '../../providers/city-data/city-data';

declare let BMap;
@IonicPage()
@Component({
  selector: 'page-over-details',
  templateUrl: 'over-details.html',
})
export class OverDetailsPage {
  testData: QueOutput = this.navParams.get("resData");

  listRes: string;
  data = {};
  res = [];
  text: string;
  Que;
  num;
  answerlog: AnswerLog;
  cityColumns: any[];
  constructor(public navParams: NavParams,
              private api: Api,
              public toastCtrl: ToastController,
              public navCtrl: NavController,
              public alertCtrl: AlertController,
              private renderer: Renderer2,
              private app: App,
              public cityDataProvider: CityDataProvider,
              public localbasefun: localbase
  ) {}
  @ViewChild('subt') subt: ElementRef;
  z;
  y;
  tt = false;
  ID;
  _I;
  show;
  id;
  time = [];
  subbtncolor;
  answertime;
  FSummitTime;
  initopic() {
    this.cityColumns = this.cityDataProvider.cities;
    let listTopic = this.testData.Que.listTopic;
    let listanswertopic = this.answerlog.TopicResult;
     this.answertime=this.answerlog.UseTime;
     this.FSummitTime=this.answerlog.FSummitTime;

    //隐藏
    listTopic.forEach((onetopic, index) => {
      onetopic.isComplte = true;
      switch (onetopic.FType) {
        case "10001": onetopic.typename = "单选题"; break;
        case "10002": onetopic.typename = "多选题"; break;
        case "10003": onetopic.typename = "填空题"; break;
        case "10004": onetopic.typename = "主观题"; break;
        case "10005": onetopic.typename = "矩阵题"; break;
        case "10006": onetopic.typename = "打分题"; break;
        case "10007": onetopic.typename = "地区题"; break;
        case "10010": onetopic.typename = "时间题"; break;
        case "10011": onetopic.typename = "排序题"; break;
        case "10012": onetopic.typename = "投票题"; break;
        case "10013": onetopic.typename = "图片题"; break;
        case "10015": onetopic.typename = "下拉题"; break;
        case "10016": onetopic.typename = "比重题"; break;
        case "10017": onetopic.typename = "联动题"; break;
        case "10018": onetopic.typename = "测试选择题"; break;
        case "10020": onetopic.typename = "测试填空题"; break;
        case "30012": onetopic.typename = "行业题"; break;
        case "30014": onetopic.typename = "地图题"; break;
        default: onetopic.typename = "单选题";
      }

    })
    listanswertopic.forEach((oneanswertopic, index) => {

      if (listTopic.filter(p => p.FTopicId == oneanswertopic.FTopicId) != undefined && listTopic.filter(p => p.FTopicId == oneanswertopic.FTopicId) != null) {
        const thistopic = listTopic.filter(p => p.FTopicId == oneanswertopic.FTopicId)[0];
        thistopic.FShow = oneanswertopic.FShow;
        thistopic.FImage = oneanswertopic.FImage;
        thistopic.FAudio = oneanswertopic.FAudio;
        switch (oneanswertopic.FType) {
          case "10005":
            if (thistopic.FAnswerForm == '1000506') {//矩阵滑块
              oneanswertopic.listChild.forEach((onea, index) => {
                // //console.log(listRow)
                thistopic.listRow.forEach((oner, indexr) => {
                  if (onea.FROptionId == oner.FROptionId) {
                    oner.TemResult = onea.FTextContent;
                  }
                })
              })
            }
            if (thistopic.FAnswerForm == '1000501') {//矩阵单选
              thistopic.listRow.forEach((oner, indexr) => {
                thistopic.listColumn.forEach((onen, indexr) => {
                  oneanswertopic.listChild.forEach((onea, index) => {
                    if (onea.FROptionId == oner.FROptionId && onea.FCOptionSort == onen.FSort.toString()) {
                      oner.TemResult = oner.FSort + '.' + onen.FSort;
                      //console.log(oner.TemResult)
                    }
                  })
                })
              })
            }
            if (thistopic.FAnswerForm == '1000502') {//矩阵多选

              //
              thistopic.listRow.forEach(oner => {
                oner.listColumn = [];
                thistopic.listColumn.forEach(onec => {
                  let thisTemResult = "";
                  oneanswertopic.listChild.forEach((onea, indexa) => {
                    if (onea.FROptionId == oner.FROptionId && onea.FCOptionSort == onec.FSort.toString()) {
                      if (onea.FROptionSort) {
                        thisTemResult = "true";
                      } else {
                        thisTemResult = 'false';
                      }
                    }
                  })
                  let tempc = new ColumnInfo();
                  tempc.FCOptionAvg = "";
                  tempc.FCOptionContent = onec.FCOptionContent;
                  tempc.FCOptionId = onec.FCOptionId;
                  tempc.FQId = onec.FQId;
                  tempc.FSort = onec.FSort;
                  tempc.FTopicId = onec.FTopicId;
                  tempc.isChecked = thisTemResult;
                  oner.listColumn.push(tempc);
                });
              })
            }
            if (thistopic.FAnswerForm == '1000503') {//矩阵下拉
              thistopic.listRow.forEach(oner => {
                oner.listColumn = [];
                thistopic.listColumn.forEach(onec => {
                  //
                  let thisTemResult = "";
                  oneanswertopic.listChild.forEach((onea, indexa) => {
                    if (onea.FROptionId == oner.FROptionId && onea.FCOptionSort == onec.FSort.toString()) {
                      thisTemResult = onea.FTextContent;
                    }
                  })
                  //
                  let tempc = new ColumnInfo();
                  tempc.FCOptionAvg = "";
                  tempc.FCOptionContent = onec.FCOptionContent;
                  tempc.FCOptionId = onec.FCOptionId;
                  tempc.FQId = onec.FQId;
                  tempc.FSort = onec.FSort;
                  tempc.FTopicId = onec.FTopicId;
                  // tempc.TemResult=thisTemResult;
                  tempc.Dropdowns = [];
                  let isfirst1000505 = true;//
                  thistopic.Dropdowns.forEach(oned => {
                    if (oned.FCSort == onec.FSort) {
                      tempc.Dropdowns.push(oned);
                      if (isfirst1000505) {
                        tempc.TemResult = thisTemResult;
                      }
                      isfirst1000505 = false;
                    }

                  })
                  oner.listColumn.push(tempc);
                })
              });

            }
            if (thistopic.FAnswerForm == '1000504') {//矩阵填空

              //
              thistopic.listRow.forEach(oner => {
                oner.listColumn = [];
                thistopic.listColumn.forEach(onec => {
                  let thisTemResult = "";
                  oneanswertopic.listChild.forEach((onea, indexa) => {
                    if (onea.FROptionId == oner.FROptionId && onea.FCOptionSort == onec.FSort.toString()) {
                      if (onea.FROptionSort) {
                        thisTemResult = onea.FTextContent;
                      } else {
                        thisTemResult = '';
                      }
                    }
                  })
                  let tempc = new ColumnInfo();
                  tempc.FCOptionAvg = "";
                  tempc.FCOptionContent = onec.FCOptionContent;
                  tempc.FCOptionId = onec.FCOptionId;
                  tempc.FQId = onec.FQId;
                  tempc.FSort = onec.FSort;
                  tempc.FTopicId = onec.FTopicId;
                  tempc.TemResult = "";
                  tempc.FTempResult = thisTemResult;
                  oner.listColumn.push(tempc);
                });
              })
            }
            if (thistopic.FAnswerForm == '1000505') {//矩阵打分
              thistopic.listRow.forEach(oner => {
                oner.listColumn = [];
                thistopic.listColumn.forEach(onec => {
                  //
                  let thisTemResult = "";
                  oneanswertopic.listChild.forEach((onea, indexa) => {
                    if (onea.FROptionId == oner.FROptionId && onea.FCOptionSort == onec.FSort.toString()) {
                      thisTemResult = onea.FTextContent;
                    }
                  })
                  //
                  let tempc = new ColumnInfo();
                  tempc.FCOptionAvg = "";
                  tempc.FCOptionContent = onec.FCOptionContent;
                  tempc.FCOptionId = onec.FCOptionId;
                  tempc.FQId = onec.FQId;
                  tempc.FSort = onec.FSort;
                  tempc.FTopicId = onec.FTopicId;
                  // tempc.TemResult=thisTemResult;
                  tempc.Dropdowns = [];
                  let isfirst1000505 = true;//
                  thistopic.Dropdowns.forEach(oned => {
                    if (oned.FCSort == onec.FSort) {
                      tempc.Dropdowns.push(oned);
                      if (isfirst1000505) {
                        tempc.TemResult = thisTemResult;
                      }
                      isfirst1000505 = false;
                    }

                  })
                  oner.listColumn.push(tempc);
                })
              });

            }
            break;
          case "10016"://比重
            //console.log(thistopic)
            oneanswertopic.listChild.forEach((onea, index) => {
              thistopic.listRow.forEach((oner, indexr) => {
                if (onea.FROptionId == oner.FROptionId) {
                  let data;
                  data = onea.FTextContent;
                  oner.TemResult = data;
                  oner.TemInput = (data / thistopic.FScore / 0.01).toFixed(0)
                }
              })
            })
            break;
          case "10001"://单选题
            if (oneanswertopic.listChild.length == 0) {
              thistopic.TempResult = "";
            }
            else {
              thistopic.TempResult = oneanswertopic.listChild[0].FROptionId;
            }
            break;
          case "30014":
            thistopic.Address = this.answerlog.Address;
            //获取手机经纬度
            if (this.answerlog.Gps != "") {
              let temparrll = this.answerlog.Gps.split(',');
              thistopic.longitude = temparrll[0];
              thistopic.lat = temparrll[1];
            }
            else {
              //获取手机经纬度
              var geolocation = new BMap.Geolocation();
              geolocation.getCurrentPosition(function (r) {
                thistopic.longitude = r.point.lng;
                thistopic.lat = r.point.lat;
              })
              if (thistopic.longitude == undefined || thistopic.longitude == null || thistopic.longitude == "") {
                thistopic.longitude = "126.691392";
                thistopic.lat = "45.75136";
              }
            }

            break;
          case "30012"://行业题
            if (oneanswertopic.listChild.length == 0) {
              thistopic.TempResult = "";
            }
            else {
              thistopic.TempResult = oneanswertopic.listChild[0].FROptionId;
            }
            break;
          case "10012"://投票题
            if (oneanswertopic.listChild.length == 0) {
              thistopic.TempResult = "";
            }
            else {
              thistopic.TempResult = oneanswertopic.listChild[0].FROptionId;
            }
            break;
          case "10011"://排序题
            thistopic.listRow.forEach((oner, indexr) => {
              oneanswertopic.listChild.forEach(oneans => {
                if (oneans.FROptionId == oner.FROptionId) {
                  oner.SortQ = parseInt(oneans.FTextContent);
                }
              });

            });
            break;
          case "10007"://地区题
            thistopic.regtemplateActive = true;
            thistopic.regtext = oneanswertopic.TempResult;
            ;
            break;
          case "10017"://联动题
            let TempResult10017arr = oneanswertopic.TempResult.split('-');
            thistopic.listColumn.forEach((onec, indexc) => {
              onec.Dropdowns = [];
              let temonedq = new DropdownInfo();
              temonedq.FDOptionId = "-1";
              temonedq.FOptionContent = "请选择" + onec.FCOptionContent;
              temonedq.FCSort = 0;
              temonedq.FCid = onec.FCOptionId;
              temonedq.FPid = "-1";
              onec.Dropdowns.push(temonedq);
              // if(onec.FSort==1){
              thistopic.Dropdowns.forEach(oned => {
                if (oned.FCid == onec.FCOptionId) {
                  onec.Dropdowns.push(oned);
                }
              });
              onec.FTempResult = TempResult10017arr[indexc];
            });
            ;
            break;

          case "10018"://测试单选题
            if (thistopic.FselectItemNum == 0) {
              if (oneanswertopic.listChild.length == 0) {
                thistopic.TempResult = "";
              }
              else {
                thistopic.TempResult = oneanswertopic.listChild[0].FROptionId;
              }
            } else {
              if (oneanswertopic.listChild.length == 0) {
                oneanswertopic.listChild.length = 0;
              } else {
                let FTextContent10018 = oneanswertopic.listChild[0].FTextContent;
                let FTextContent10018arr = FTextContent10018.split('||');
                thistopic.listRow.forEach(p => {
                  p.testChecked = false;
                  FTextContent10018arr.forEach(pickedid => {
                    if (pickedid == p.FROptionId) {
                      p.testChecked = true;
                    }
                  })
                });
              }

            }
            break;
          case "10002":
            if (oneanswertopic.listChild.length == 0) {
              oneanswertopic.listChild.length = 0;
            } else {
              let FTextContent10002 = oneanswertopic.listChild[0].FTextContent;
              let FTextContent10002arr = FTextContent10002.split('||');
              thistopic.listRow.forEach(p => {
                p.isChecked = false;
                FTextContent10002arr.forEach(pickedid => {
                  if (pickedid == p.FROptionId) {
                    p.isChecked = true;
                  }
                })
              })
            }

            break;

          case "10004":
            thistopic.subjectivity = oneanswertopic.listChild[0].FTextContent;
            break;

          case "10015":
            if (oneanswertopic.listChild.length == 0) {
              thistopic.isSelected = "";
            } else {
              thistopic.isSelected = oneanswertopic.listChild[0].FROptionId;
            }


            break;

          case "10013":
            if (oneanswertopic.listChild.length == 0) {
              thistopic.imgs = ""
            } else {
              thistopic.imgs = oneanswertopic.listChild[0].FROptionId;
            }

            break;

          case "10003":
            if (oneanswertopic.listChild.length == 0) {
              oneanswertopic.listChild[0].FTextContent = "";
            } else {
              let value10003 = oneanswertopic.listChild[0].FTextContent.split("||");
              for (var i = 0; i < thistopic.completion.children.length; i++) {
                thistopic.completion.children[i].value = value10003[i];
              }
            }
            ;
            break;
          case "10020":
            let value10020 = oneanswertopic.listChild[0].FTextContent.split("||");
            for (var j = 0; j < thistopic.testCompletion.children.length; j++) {
              thistopic.testCompletion.children[j].value = value10020[j];
            }
            break;
          case '10010':
            if (oneanswertopic.listChild.length == 0) {
              oneanswertopic.listChild.length = 0;
            } else {
              let value10010 = oneanswertopic.listChild[0].FTextContent;

              let value10010arr = value10010.split('||');

              value10010arr.forEach(onevalue => {

                if (onevalue.indexOf("年") != -1) {
                  thistopic.timeyear = onevalue.substr(0, onevalue.length - 1);
                }
                if (onevalue.indexOf("月") != -1) {
                  thistopic.timemonth = onevalue.substr(0, onevalue.length - 1);
                  thistopic.timemonth = onevalue.substr(1, 1);
                }
                if (onevalue.indexOf("日") != -1) {
                  thistopic.timeday = onevalue.substr(0, onevalue.length - 1);
                  thistopic.timeday = onevalue.substr(1, 1);
                }
                if (onevalue.indexOf("时") != -1) {
                  thistopic.timehour = onevalue.substr(0, onevalue.length - 1);
                  thistopic.timehour = onevalue.substr(1, 1);
                }
                if (onevalue.indexOf("分") != -1) {
                  thistopic.timeminite = onevalue.substr(0, onevalue.length - 1);
                  thistopic.timeminite = onevalue.substr(1, 1);
                }
                if (onevalue.indexOf("秒") != -1) {
                  thistopic.timesecond = onevalue.substr(0, onevalue.length - 1);
                  thistopic.timesecond = onevalue.substr(1, 1);
                }
              });
              break;
            }
        }
      }
    })
  }
  async ionViewDidEnter() {


    let that = this;
    var data = this.navParams.get("resData");
    this.id = this.navParams.get("s");
    this._I = this.navParams.get("answerid");
    this.num = this.navParams.get("num");

    this.Que = data.Que;
    this.res = data.Que.listTopic;
    this.ID = data.Que.FQId;

    //获取作答数据
    const localbasefun = this.localbasefun;
    this.answerlog = await localbasefun.getbaseAnswerlogByanswerid(this._I);

    this.initopic();
    this.listRes = data.Que.FTitle;
    // console.log(55555555555555555)
    // console.log(data.Que)
    this.subbtncolor = "grey";
  }

}
